package com.example.myapplication

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import com.example.myapplication.tutorial.ListSection
import com.facebook.litho.Column
import com.facebook.litho.Component
import com.facebook.litho.ComponentContext
import com.facebook.litho.Handle
import com.facebook.litho.LithoView
import com.facebook.litho.sections.SectionContext
import com.facebook.litho.sections.widget.RecyclerCollectionComponent
import com.fenbi.tutor.live.module.large.large.chat.lithoview.BanUnbanMsgInfo
import com.fenbi.tutor.live.module.large.large.chat.lithoview.ChatMsgSectionSpec
import com.fenbi.tutor.live.module.large.large.chat.lithoview.MsgInfo
import com.fenbi.tutor.live.module.large.large.chat.lithoview.NormalMsgInfo
import com.fenbi.tutor.live.module.large.large.chat.lithoview.RootComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlin.coroutines.CoroutineContext
import kotlin.random.Random

class MainActivity : Activity() {

    var banedAll = false
    var banedU = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        genTutorial()
    }

    fun doV() {

        setContentView(R.layout.activity_main)

        val frame = findViewById<FrameLayout>(R.id.frame)

        val addMsg = findViewById<Button>(R.id.add_msg)
        val banUnbanAll = findViewById<Button>(R.id.ban_unban_all)
        val banUnbanU = findViewById<Button>(R.id.ban_unban_u)
        addMsg.setOnClickListener {
            msgList.add(
                NormalMsgInfo(
                    SpannableStringBuilder().append(randomStr(8))
                        .append(" : ")
                        .append(randomStr(32))
                )
            )
            updateView(this)
        }
        banUnbanAll.setOnClickListener {
            msgList.add(
                BanUnbanMsgInfo(
                    SpannableStringBuilder().append("all ban/unbaned")
                )
            )
            updateView(this)

        }
        banUnbanU.setOnClickListener {
            msgList.add(
                BanUnbanMsgInfo(
                    SpannableStringBuilder().append("u are ban/unbaned")
                )
            )
            updateView(this)
        }

        genV(this, frame)
    }

    val handle = Handle()
    fun genTutorial() {
        val sc = SectionContext(this)
        setContentView(
            LithoView.create(
                this,
                RecyclerCollectionComponent.create(ComponentContext(this))
                    .disablePTR(true)
                    .handle(handle)
                    .section(ListSection.create(sc).build())
                    .build()
            )
        )

        ListSection.onTrigger(sc, handle, 3)


    }


    companion object {

        fun randomStr(len: Int): String {
            val sb = StringBuffer()
            val random = Random('Z' - 'A')
            repeat(len) {
                val c = 'A' + random.nextInt()
                sb.append(c)
            }
            return "$sb"
        }

        val msgList = mutableListOf<MsgInfo>()

        val handle: Handle = Handle()

        init {
            repeat(4) {
                msgList.add(
                    0, NormalMsgInfo(
                        "asd"
                    )
                )
            }
        }

        fun updateView(context: Context) {
            lithoView.setComponent(genComponentOfMsgList(context))
        }

        fun triggerAddMsgEvent(msg: MsgInfo) {
            //            RootComponent.onNewMsg()
        }

        fun genComponentOfMsgList(context: Context): Component {
            val newList = msgList.map {
                when (it) {
                    is NormalMsgInfo -> NormalMsgInfo(it.msg)
                    is BanUnbanMsgInfo -> BanUnbanMsgInfo(it.msg)
                    else -> NormalMsgInfo("")
                }
            }
            val c = ComponentContext(context)
            val component = RootComponent.create(c)
                .handle(handle)
                .build()
            return component
        }

        lateinit var lithoView: LithoView


        fun genV(context: Context, root: ViewGroup) {
            lithoView = LithoView.create(context, genComponentOfMsgList(context))
            root.addView(
                lithoView,
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
        }
    }
}